﻿package  
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/*
	 * 改行で区切られたテキストを "aaa","bbb","ccc" … のカタチにする
	 */
	public class TxtParse 
	{
		
		private var _urlloader :URLLoader;
		
		// Shift-jis
		
		public function TxtParse() 
		{
			
		}
		
		public function load ( path :String ) :void
		{
			_urlloader = new URLLoader();
			_urlloader.addEventListener(Event.COMPLETE, _loadComplete);
			
			_urlloader.load( new URLRequest(path) );
			
		}
		public function parse ( text :String ) :String
		{
			return _parse( text );
		}
		
		private function _loadComplete ( evt :Event ) :void 
		{
			_urlloader.removeEventListener(Event.COMPLETE, _loadComplete);
			
			
			var str :String = _urlloader.data as String;
			str = _parse( str );
			
			trace( str );
		}
		
		private function _parse ( str :String ) :String
		{
			str = str.split("\r\n").join("\n");
			str = str.split("\r").join("\n");
			
			var arr :/*String*/Array = str.split("\n");
			
			var s  :String = "";
			var s2 :String = "";
			var s3 :String = "";
			
			
			for ( var i :int = 0; i < arr.length; i ++ )
			{
				var a :/*String*/Array = arr[i].split(",");
				
				if ( 3 <= a.length )	
				{
					s3 += a[0] + ',';
					s2 += a[1] + ',';
					s  += '"' + a[2] + '",';
				}
				else if ( 2 <= a.length )	
				{
					s3 += '1.5,';
					s2 += a[0] + ',';
					s  += '"' + a[1] + '",';
				}
				else
				{
					s3 += '1.5,';
					s2 += '32,';
					s  += '"' + arr[i] + '",';
				}
			}
			s  =  s.slice(0,  s.length - 1);
			s2 = s2.slice(0, s2.length - 1);
			s3 = s3.slice(0, s3.length - 1);
			
			
			return s + "\n" + s2 + "\n" + s3;
		}
		
	}
}